package com.hlingsoft.xinsuo.action;

import com.hlingsoft.xinsuo.dao.LockDao;
import com.hlingsoft.xinsuo.dao.VerifyCodeDao;
import com.hlingsoft.xinsuo.entity.Lock;
import com.hlingsoft.xinsuo.entity.Result;
import com.hlingsoft.xinsuo.entity.VerifyCode;
import com.hlingsoft.xinsuo.service.MessageService;
import com.hlingsoft.xinsuo.util.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by selbylei on 2015/9/23.
 */
@Controller
@RequestMapping("/custom")
public class CustomAction {

    @Autowired
    private VerifyCodeDao verifyCodeDao;

    @Autowired
    private MessageService messageService;

    @Autowired
    private LockDao lockDao;

    /**
     * 获取验证码
     * @param phone
     * @param request
     * @return
     */
    @RequestMapping(value = "/verifyCode.json")
    @ResponseBody
    public Map<String, Object> verifyCode(
            @RequestParam(value = "phone") String phone,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            String str = Common.Randoms();
            resultMap.put("result", true);
            try {
                VerifyCode verifyCode = verifyCodeDao.getVerifyCodeByPhone(phone);
                Result result = messageService.sendSmsforEvent(phone, str);
                //短信接口请求成功
                if (result.getCode().equals("2")) {
                    //新用户，插入数据表
                    if (verifyCode == null) {
                        VerifyCode verifyCode1 = new VerifyCode();
                        verifyCode1.setPhone(phone);
                        verifyCode1.setCode(str);
                        verifyCodeDao.addVerifyCode(verifyCode1);
                    }
                    //老用户，更新数据表
                    else {
                        verifyCode.setPhone(verifyCode.getPhone());
                        verifyCode.setCode(str);
                        verifyCodeDao.updateVerifyCode(verifyCode);
                    }
                    resultMap.put("msg", result.getMsg());
                    resultMap.put("code", result.getCode());
                    resultMap.put("verifyCode", str);
                }
                //手机号码不对
                else if (result.getCode().equals("406")) {
                    resultMap.put("msg", result.getMsg());
                    resultMap.put("code", result.getCode());
                }
                //其他调用短信接口异常
                else {
                    if (verifyCode != null) {
                        resultMap.put("verifyCode", verifyCode.getCode());
                    }
                    resultMap.put("msg", result.getMsg());
                    resultMap.put("code", result.getCode());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("msg", "接口调用失败");
            resultMap.put("result", false);
        }
        return resultMap;
    }




    /**
     * 解锁获取验证码
     * @param phone
     * @param request
     * @return
     */
    @RequestMapping(value = "/verifyCodeB.json")
    @ResponseBody
    public Map<String, Object> verifyCodeB(
            @RequestParam(value = "phone") String phone,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            String str = Common.Randoms();
            try {
                VerifyCode verifyCode = verifyCodeDao.getVerifyCodeByPhone(phone);
                List<Lock> lockList = lockDao.findByPhoneAndState(phone, 1);
                if (lockList.size()>0){
                    Result result = messageService.sendSmsforEvent(phone, str);
                    //短信接口请求成功
                    if (result.getCode().equals("2")) {
                        //新用户，插入数据表
                        if (verifyCode == null) {
                            VerifyCode verifyCode1 = new VerifyCode();
                            verifyCode1.setPhone(phone);
                            verifyCode1.setCode(str);
                            verifyCodeDao.addVerifyCode(verifyCode1);
                        }
                        //老用户，更新数据表
                        else {
                            verifyCode.setPhone(verifyCode.getPhone());
                            verifyCode.setCode(str);
                            verifyCodeDao.updateVerifyCode(verifyCode);
                        }
                        resultMap.put("msg", result.getMsg());
                        resultMap.put("code", result.getCode());
                        resultMap.put("verifyCode", str);
                    }
                    //手机号码不对
                    else if (result.getCode().equals("406")) {
                        resultMap.put("msg", result.getMsg());
                        resultMap.put("code", result.getCode());
                    }
                    //其他调用短信接口异常
                    else {
                        if (verifyCode != null) {
                            resultMap.put("verifyCode", verifyCode.getCode());
                        }
                        resultMap.put("msg", result.getMsg());
                        resultMap.put("code", result.getCode());
                    }
                }else {
                    resultMap.put("msg","用户没有上锁设备");
                    resultMap.put("result", false);
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("msg", "接口调用失败");
            resultMap.put("result", false);
        }
        return resultMap;
    }



    /**
     * 心锁登陆
     *
     * @param phone
     * @param request
     * @return
     */
    @RequestMapping(value = "/login.json")
    @ResponseBody
    public Map<String, Object> login(
            @RequestParam(value = "phone") String phone,
            @RequestParam(value = "verifyCode") String verifyCode,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            VerifyCode vc = verifyCodeDao.getVerifyCodeByPhone(phone);
            List<Lock> lockList = lockDao.findByPhone(phone);
            Date now = new Date();
            if (lockList.size() > 0) {
                if (vc!=null){
                  if (vc.getCode().equals(verifyCode)){
                      for (Lock lock:lockList){
                          long diff = now.getTime() - lock.getCreateTime().getTime();
                          long diffDays = diff / (1000 * 60 * 60 * 24)+1;
                          lock.setDiffLockDays(diffDays);
                      }
                      resultMap.put("lockList",lockList);
                      resultMap.put("msg","登陆成功");
                      resultMap.put("result",true);
                  }else {
                      resultMap.put("msg","验证码输入有误");
                      resultMap.put("result",false);
                  }
                }   else {
                    resultMap.put("msg","请先获取验证码");
                    resultMap.put("result",false);
                }

            } else {
                if (vc!=null){
                    if (vc.getCode().equals(verifyCode)){
                        resultMap.put("msg","登陆成功,用户未绑定设备");
                        resultMap.put("result",true);
                    }else {
                        resultMap.put("msg","验证码输入有误");
                        resultMap.put("result",false);
                    }
                }else {
                    resultMap.put("msg","请先获取验证码");
                    resultMap.put("result",false);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }




    /**
     * 用户退出登陆
     * 退出时检测是否有解锁的心锁
     *
     * @param phone
     * @param devId
     * @param request
     * @return
     */
    @RequestMapping(value = "/logout.json")
    @ResponseBody
    public Map<String, Object> logout(
            @RequestParam(value = "phone") String phone,
            @RequestParam(value = "devId") String devId,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            resultMap.put("result", true);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }



}
